// pages/signin/signin.js
Page({
    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        state: true,
        dateData: [],
        isSignin: false,
        week: ['日', '一', '二', '三', '四', '五', '六'],
        time: [],
        stare: false,
        text: '签到',
        arr:['补签需要消耗15点积分！','连续签到一月额外奖励100积分','积分可以兑换超多实用商品！']
    },
    //用户补签
    mendSignIn(e){
       var mendSignInDay=e.target.id
       var token = wx.getStorageSync('token')
        var uid = wx.getStorageSync('uid')
        var that=this
        var list=this.data.dateData;
        wx.request({
          url: 'http://fbj.wdmnm.vip/api/mendSignIn',
          data:{
              uid:uid,
              token:token,
              mendSignInDay:mendSignInDay
          },
          dataType:'json',
          success:function(res){
            if (res.data.code != 200) {
                wx.showToast({
                    title: '补签失败',
                    icon: 'none',
                    dateData: 2000
                })
            }else{
                for (let i = 0; i < list.length; i++) {
                    if (mendSignInDay == list[i].day && list[i].type == "curr") {
                        // console.log(i)
                        // 当天样式变化
                        list[i].chk = true;
                        break;
                    }
                }
                wx.showToast({
                  title: '补签成功',
                  icon:'success',
                  dateData:2000,
                  success:function(res){
                      that.setData({
                        dateData: list,
                        stare: true,
                      })
                  }
                })
            }
          }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.initCurrMonthData();
        let currDate = new Date(); //当前日期
        let day = currDate.getDate();
        var that = this
        var token = wx.getStorageSync('token')
        var uid = wx.getStorageSync('uid')
        var list = that.data.dateData;

        //判断当前用户是否登录
        if (!uid && !token) {
            wx.showToast({
                title: '请先登录',
                icon: 'none',
                dateData: 2000
            })
            return false
        }
        // console.log(list);
        wx.request({
            url: 'http://fbj.wdmnm.vip/api/getUserSignList',
            data: {
                uid: uid,
                token: token
            },
            dataType: 'json',
            success: function (res) {
                if (res.data.code != 200) {
                    wx.showToast({
                        title: '用户签到信息获取失败，请稍后再试',
                        icon: 'none',
                        dateData: 2000
                    })
                }
                for (let i in list) {
                    for (let key in res.data.data) {
                        // console.log(key);
                        if (list[i].type == 'curr' && list[i].day == key) {
                            if (res.data.data[key] == 0) {
                                list[i].chk = false
                            } else {
                                if (list[i].day == day) {
                                    console.log('sadsgfd');
                                    that.setData({
                                        stare: true,
                                        text: '今日已签到'
                                    })
                                }
                                list[i].chk = true
                            }
                        }
                    }
                }
                that.setData({
                    dateData: list
                })
            }
        })
    },

    monthDays(year, month) {
        // console.log(year,month)
        let days_count = new Date(year, month, 0).getDate() //月总天数 如:31
        let days = []; //存放月的天数
        for (let i = 1; i <= days_count; i++)
            days.push(i)
        return days;
    },

    //初始化当月数据
    initCurrMonthData() {
        let currDate = new Date(); //当前日期
        // 当前月 上个月 这个月的1号是星期几
        let currMonthDays = this.monthDays(currDate.getFullYear(), currDate.getMonth() + 1);
        let lastMonthDays = this.monthDays(currDate.getFullYear(), currDate.getMonth());
        let currFirstWeek = new Date(currDate.getFullYear(), currDate.getMonth() - 1, 1).getDay() - 4;
        let dateData = [];
        dateData = currMonthDays.map(val => this.formatDay(val)); //当月的数据
        for (let i = 0; i < currFirstWeek; i++) {
            //上月要显示的
            dateData.unshift(
                this.formatDay(lastMonthDays.pop(), 'last')
            );
        }

        let nextLenth = 42 - dateData.length; // 42是因为 6 * 7格式
        for (var i = 1; i <= nextLenth; i++) {
            //下个月需要显示的日期
            dateData.push(
                this.formatDay(i, 'next')
            );
        }

        this.setData({
            dateData: dateData
        });
    },

    formatDay(day, type = 'curr') { //日期数据的格式化
        return {
            day: day,
            type: type,
            chk: false
        };
    },

    onRule() {
        let currDate = new Date(); //当前日期
        let day = currDate.getDate();
        let list = this.data.dateData;
        var that = this
        var uid = wx.getStorageSync('uid')
        //判断用户是否登录
        if(!uid){
            wx.showToast({
              title: '请您先登录',
              icon:'error',
              dateData:2000
            })
            return false
        }
        //请求后台接口签到
        wx.request({
            url: 'http://fbj.wdmnm.vip/api/signIn',
            data: {
                uid:uid,
                token:wx.getStorageSync('token')
            },
            dataType: 'json',
            success: function (res) {
                console.log(res);
                if (res.data.code == 200) {
                    // 查询元素在数组中的索引值
                    for (let i = 0; i < list.length; i++) {
                        if (day == list[i].day && list[i].type == "curr") {
                            // console.log(i)
                            // 当天样式变化
                            list[i].chk = true;
                            break;
                        }
                    }
                    wx.showToast({
                      title: '签到成功',
                      icon:'success',
                      dateData:2000,
                      success:function(res){
                          that.setData({
                            dateData: list,
                            stare: true,
                          })
                      }
                    })
                }
            }
        })
        // wx.showToast({
        //     title: '签到成功',
        //     icon: 'none'
        // });
        // this.setData({
        //     dateData: list,
        //     state: false,
        // });
        // wx.setStorage({
        //     key: 'myData',
        //     data: this.data.dateData
        // });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        let day = new Date();
        day = new Date(day.getFullYear(), day.getMonth() + 1, 0).getDate();
        this.setData({
            second: day
        });
        let that = this;
        wx.getStorage({
            key: 'myData',
            success: function (res) {
                that.setData({
                    dateData: res.data
                });
                let arr = res.data;
                let a = 0;
                let b = that.data.second;
                for (let i = arr.length - 1; i > 0; i--) {
                    if (arr[i].type == "curr" && arr[i].chk == true && arr[i - 1].chk == true) {
                        a++;
                        b--;
                        console.log(1)
                    } else if (arr[i].type == "curr" && arr[i].chk == true) {
                        a = 1;
                        b--;
                        break;
                    } else {
                        that.setData({
                            state: true
                        });
                    }
                }

                if (a > 0) {
                    that.setData({
                        state: false
                    })
                }
                that.setData({
                    first: a,
                    second: b
                })
            }
        });
    }
})